// pages/product/product.js
var Tool = require('../../config/tool.js');
var WxParse = require('../../wxParse/wxParse.js');
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';

const app = getApp()
//团购详情
import { getDisteData, setLogin } from '../../config/https'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeSta: "",                       //判断是否已经结束团购时间
    is_join: '',                       //是否参团0没有1有
    height: '',                        //轮播高度
    numOrder: '',                      //团购数量
    // show:false,                    //参数弹出状态
    text: [],                          //测试倒计时
    product: {},                      // 商品数据
    // trueto: true,                  //是否收藏
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    const userInfo = wx.getStorageSync("authKey");
    if (!userInfo) {
      wx.login({
        success(res) {
          console.log(res)
          var code = res.code
          setLogin({
            code: code,
            // user_json: userInfo
          }).then(res => {
            console.log(res)
            app.globalData.stastaVip = res.version
            app.globalData.userInfo = res.auth_key
            wx.setStorage({
              key: 'authKey',
              data: res.auth_key,
            })
          }).catch(res => {
            console.log(res)
          })
        }
      })
    }
    console.log(Number('06') > Number('00'))
    var that = this;
    //上个页面的商品id
    if (options.id) {
      that.setData({
        id: options.id
      })
    }
  },
  onLogin() {

  },
  //获取轮播高度
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"
    this.setData({
      height: swiperH //设置高度
    })
  },
  //商品详情
  onDatsData() {
    var that = this
    getDisteData({
      id: that.data.id
    }).then(res => {
      //结束时间
      var createtime = res.goods.endtime;
      //现在的时间
      var timestamp = Date.parse(new Date()) / 1000;
      //剩余倒计时
      let last = createtime - timestamp
      clearInterval(that.time);
      //团购倒计时开始
      that.time = setInterval(() => {
        var text = Tool.count(last ? last : 0);
        that.setData({ text })
        if (last > 0) {
          last--;
          that.setData({ typeSta: true })
        } else {
          that.setData({ typeSta: false })
        }
      }, 1000);
      var numOrder = res.orders
      that.setData({
        numOrder
      })
      var orderSeta = res.orders
      if (orderSeta.length > 6) {
        orderSeta.length = 6
      }
      that.setData({
        orderid: res.owner_order.id,
        teamid: res.owner_order.teamid,
        is_join: res.is_join,
        goods: res.goods,
        orderList: orderSeta,
      })
      WxParse.wxParse('article', 'html', res.goods.content, that, 5);
    }).catch(err => {
      Toast(err.message)
    })
  },

  onReady: function () {//生命周期函数--监听页面初次渲染完成

  },


  onShow: function () {//生命周期函数--监听页面显示
    this.onDatsData()
  },

  // 跳转确定订单页面
  onPurOrders(e) {
    if (!app.globalData.logins) {
      wx.navigateTo({
        url: '/pages/logs/logs',
      })
      return
    }
    let typeSta = this.data.typeSta
    if (!typeSta) {
      Toast("当前活动已经结束")
      return
    }
    if (this.data.goods.groupnum - this.data.goods.fightnum == 0) {
      Toast("当前活动已经结束")
      return
    }
    if (this.data.is_join == 1) {
      Toast("您已参与本商品的拼团~~")
      return
    }
    // if ((text[2] == '00' && text[1] == '00' && text[0] == '00') || Number(text[0]) < Number(00) ){
    //   Toast("抱歉当前活动已经结束")
    //   return
    // }
    wx.navigateTo({
      url: '/pages/shoppurorders/shoppurorders?id=' + e.currentTarget.dataset.id,
    })
  },
  //跳转购买记录
  onRecord() {
    if (this.data.numOrder.length > 0) {
      wx.navigateTo({
        url: '/pages/purchaseRecord/purchaseRecord?id=' + this.data.id,
      })
    } else {
      Toast("还没有人购买 快快参与吧~~")
    }

  },
  // 跳转确定订单页面已经付款
  onPuser() {
    const userInfo = wx.getStorageSync("authKey");
    if (!userInfo) {
      wx.navigateTo({
        url: '/pages/logs/logs',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/paymenstapur/paymenstapur?type=0&orderid=' + this.data.orderid + '&teamid=' + this.data.teamid,
    })
  },
  //点击弹出层显示或者隐藏
  // onClose() {
  //   let show = !this.data.show;
  //   this.setData({show});
  // },

  //点击收藏
  // shouc() {
  //   let trueto = !this.data.trueto;
  //   this.setData({ trueto })
  // },

  // 跳转首页
  onIndex() {
    wx.reLaunch({
      url: '/pages/tapswitching/tapswitching',
    })
  },
  /**
    * 生命周期函数--监听页面隐藏
    */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    let id = that.data.goods.id
    return {
      title: that.data.goods.title,  
      path: '/pages/directbuyingto/directbuyingto?id=' + id + '&type=0' + '&userid=' + wx.getStorageSync('userInfo'),
      imageUrl: that.data.goods.thumb,  //转发图片 默认当前页面图片
      success: (res) => {//转发成功
        console.log("转发成功", res);
      }
    }
  }

})